﻿using System;
using System.Threading;
using UnityEngine;
using UnityEngine.Rendering;

public class TimerService : ILogic
{
   public TickTimer tickTimer { get; private set; }
    public PrecisionTimer precisionTimer { get; private set; }
    public void OnInit()
    {
        //对接计时器：默认用Unity的Update驱动，没有分帧处理；
        //tickTimer = new TickTimer(0);
        //tickTimer.logFunc = this.Log;
        //tickTimer.warnFunc = this.Warn;
        //tickTimer.errorFunc = this.Error;

        precisionTimer = new PrecisionTimer(0);
        precisionTimer.logFunc = this.Log;
        precisionTimer.warnFunc = this.Warn;
        precisionTimer.errorFunc = this.Error;
        TestTime();

    }

    private void TestTime()
    {
        this.Log("开始计时");
       // tid= AddTimer(TimerType.RealTime, 2000, TestPlus);
        //  AddTimer(TimerType.UnscaledTime, 2000, TestUnScale);
        AddLoopTimer(TimerType.ScaledTime, 2000, TestScale, null, LoopEnd, 3);
    }

    private void LoopEnd()
    {
        this.Log("循环结束！");
    }

    int tid;

    public void OnEnterState()
    {
      
    }
    private void TestPlus()
    {
        this.Log("Precision计时器工作正常");
    }

    private void Test()
    {
        this.Log("Tick计时器工作正常");
    }
    private void TestUnScale()
    {
        this.Log("Precision计时器工作正常,基于非游戏时间");
    }

    private void TestScale()
    {
        this.Log("Precision计时器工作正常,基于游戏时间");
    }
    public void OnUpdate()
    {
        //驱动计时器
       precisionTimer.UpdateTime();

        if (Input.GetKeyDown(KeyCode.T))
        {
            TestTime();
        }
        if (Input.GetKeyDown(KeyCode.Y))
        {
           this.Log("当前计时剩余时间"+QueryRemaining(tid));
        }
        if (Input.GetKeyDown(KeyCode.U))
        {
            AdjustTimer(tid, 1000);
            this.Log("当前计时剩余时间" + QueryRemaining(tid));
        }
    }
    public void UnInit()
    {
        //清除所有计时任务
        precisionTimer.ResetTimer();
    }
    public int AddLoopTimer(TimerType timerType, int delayMs, Action taskCB, Action cancelCB = null,Action loopEndCB =null,int count = 1)
    {
        return precisionTimer.AddTimer(delayMs, taskCB, cancelCB, loopEndCB, count, timerType);
    }
    public int AddTimer(TimerType timerType, int delayMs, Action taskCB, Action cancelCB = null, int count = 1)
    {
        return precisionTimer.AddTimer(delayMs, taskCB, cancelCB,null, count, timerType);
    }
    public int AddTimer(int delay, Action taskCB, Action cancelCB = null, int count = 1)//默认用RealTime计时
    {
      return precisionTimer.AddTimer(delay,taskCB,cancelCB,count);
    }
    public void RemoveTimer(int tid)
    {
        precisionTimer.DeleteTimer(tid);
    }
    public bool AdjustTimer(int tid, int delayOffset)
    {
        return precisionTimer.AdjustTime(tid, delayOffset);
    }
    public bool StopTimer(int tid)
    {
      return precisionTimer.SetPaused(tid, true);
    }
    public bool EnableTimer(int tid)
    {
        return precisionTimer.SetPaused(tid, false);
    }
    public int QueryRemaining(int tid)
    {
        return precisionTimer.QueryRemaining(tid);
    }

}